package com.woniu.utils;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradeQueryResponse;

import com.woniu.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class PayUtil {
    private int uid;

    public void  setUid(Integer value){
        this.uid=value;
    }

    @Autowired
    private OrderService orderService;
    //appid
    private final String APP_ID = "2021000148648120";
    //应用私钥
    private final String APP_PRIVATE_KEY = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCjRlicv9uYAIXTnwYKr4HFRzTDNlWDT4TjY/nbWtjt6p+oJwiIAhKOq8EW4IkCNkaMMwSMBZ3YugBgj4X8gt4tnn5F6iHN385wYU7BhWctBuo3Lg5jZgh9WDMhXWStnqbuBJHDm/+9a/zO47UK8IqEUEwhqJ+3RHEV/4iaM+qTRRwtO8RmxDpelvzUkpByNe9RZA28+qiqoebhCnZuJmPiTysM6RjzpYzeGkmkbYgH9YX9h5DRxJp6dWaxG6LLOBQu1KXBIqEDMcaN2nHYaRzj+4ABjJFRhE4v8xS2fFehdSu1sWNHRlebHDOTTcKgdUEb64ld0sMM8bunfPhpzfQBAgMBAAECggEAeRqaa1mbJXltyfNv5ckb9hnEtu68KrN2m7rx/pO5x255kZ2yJD91SEF47OEKfouHdEH7I2ifFbuQwA50kUD4Ttn6Sx1IiY1xb8WdRskOKEWyv873eR6bOW2MLzk4jdbZdj4JaA9zdyp7z3DyPfUAqPduIKEUJjkrr+djiQzs9uHinvcqOnsCB0HGZgAT7k/1rm3Mwg1B7VGwC5rBlcKF4VcRIVVkUHKzOb3PiQGVXm9kXvDhEBPgXJudcGY2yNSjT3WVfOs3i2LICObHBFjTxrWte4p+v7X5GbbWAN4BkWbEYjuewveQd40IRWuNVjxkGd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4aMEIEXU+X8IH5lqfH/ZrEa/+Zm4SOpCNx+ppyvRxAV9qxGwx2REOVeXvsQ81V6cf0rFcIvr/SPzQKIxVkXsUxGa2xhchBFeAtGgs+ONFBryOJf2v1A5ameO2bLngxL6/hgmwcVNLKp6YUDJtbet5plF+EeVp6z8rJugIAid9IT6kHhZwu8IJqhFjVWC+JDxrn9iT5yOTc6uyW8AB6SsKRT82YbE3nLLZRCgINArVm4Pi+WOZVSPhyLEA2vIyRf8fUi4r0dcKDGSXGmKaavMfkngIsjeVxQyywIDAQAB";
    //网关，这是沙箱接口路径,正式路径为https://openapi.alipay.com/gateway.do
    private final String GATEWAY_URL = " https://openapi-sandbox.dl.alipaydev.com/gateway.do";
    private final String FORMAT = "JSON";
    //签名方式
    private final String SIGN_TYPE = "RSA2";
    //支付宝异步通知路径,付款完毕后会异步调用本项目的方法,必须为公网地址
    private final String NOTIFY_URL = "  http://z8cc5364.natappfree.cc/api/alipay/toSuccess";
    //支付宝同步通知路径,也就是当付款完毕后跳转本项目的页面,可以不是公网地址
   public  String getreturn(){
       return "http://localhost:5173/front/courseView/"+uid;
   }
    private AlipayClient alipayClient = null;
    //支付宝官方提供的接口
    public String sendRequestToAlipay(String outTradeNo, Float totalAmount, String subject,int uid) throws AlipayApiException {
        System.out.println("跳转的页面"+getreturn());
        setUid(uid);
        //获得初始化的AlipayClient
        alipayClient = new DefaultAlipayClient(GATEWAY_URL, APP_ID, APP_PRIVATE_KEY, FORMAT, CHARSET, ALIPAY_PUBLIC_KEY, SIGN_TYPE);
        System.out.println(this.getreturn());
        //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(this.getreturn());
        alipayRequest.setNotifyUrl(NOTIFY_URL);

        //商品描述（可空）
        String body = "";
        alipayRequest.setBizContent("{\"out_trade_no\":\"" + outTradeNo + "\","
                + "\"total_amount\":\"" + totalAmount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"body\":\"" + body + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
        //请求
        String result = alipayClient.pageExecute(alipayRequest).getBody();
        System.out.println("返回的结果是："+result );
        return result;
    }

    //    通过订单编号查询
    public String query(String id){
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", id);
        request.setBizContent(bizContent.toString());
        AlipayTradeQueryResponse response = null;
        String body=null;
        try {
            response = alipayClient.execute(request);
            body = response.getBody();
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        if(response.isSuccess()){
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }
        return body;
    }
}
